package com.tangyee.home.loadknowledgelist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tangyee.R;
import com.tangyee.app.MineApplication;

public class KnowledgeDetails extends AppCompatActivity implements View.OnClickListener{
    private ImageView back;
    private TextView title;
    private ProgressWebView content;
    private String URL="https://www.tangyee.com/Mobile/Knowledge/GetKnowledgeInfo?kid=";
    private String kid;
    private RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_home_knowledgedetails);
        back = (ImageView) findViewById(R.id.backTohome);
        back.setOnClickListener(this);
        title = (TextView) findViewById(R.id.tv_itemtitle);
        title.setText(getIntent().getExtras().get("title").toString());
        kid=getIntent().getExtras().get("id").toString();
        //加载进度条
        content = (ProgressWebView) findViewById(R.id.tv_itemContent);
        //创建网络请求
        StringRequest request = new StringRequest(URL+kid, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //使用Gson解析Json数据,获取数据结合
                KnowledgeDetailsBean kdbList = new Gson().fromJson(response,new TypeToken<KnowledgeDetailsBean>(){}.getType());

                content.loadDataWithBaseURL(null,getHtmlString(kdbList),"text/html","utf-8",null);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue= MineApplication.getRequestQueue();
        requestQueue.add(request);//将请求添加到队列中

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.backTohome:
                finish();
                break;
        }
    }
    public String getHtmlString(KnowledgeDetailsBean kdbList){
        String head="<head>\n" +
                "\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0,maximum-scale=2.0,minimum-scale=0.5, user-scalable=no,target-densitydpi=device-dpi\">\n" +
                "    <style>\n" +
                "\timg{max-width: 100%; width:auto; height:auto!important;}\n" +
                "    .pb-mt15{margin-top:15px; margin-bottom:15px;}\n" +
                "    .pb-size-tiny{font-size:12px;}\n" +
                "    .meta{line-height:12px}\n" +
                "    .at{margin-left:15px;}\n" +
                "    .vw i{background-position:0 0 }\n" +
                "    </style>\n" +
                "</head>";
        String html="<html>"+
                head+
                "<body style=\"word-wrap:break-word; background:#F3F3F3; \">"+
                "<div style=\"clear: both; content: .; display: block;\">"+
                "<h3 style=\"border-left: thick solid #21aa8e; color:#21aa8e; padding-left:10px; margin-bottom:15px; margin-top:15px; \">"+
                kdbList.getTitle()+"</h3>"+
                "</div>\n" +
                "        <div class=\"pb-mt15 pb-size-tiny meta\" style=\"margin-bottom:15px;\">\n" +
                "            <span class=\"dt\">\n" +
                "                 &nbsp;&nbsp;"+
                kdbList.getReleaseDate()+
                "</span>\n" +
                "            <span class=\"at\">发布者:"+
                kdbList.getAuthor()+
                "</span>\n" +
                "                 &nbsp;&nbsp;|&nbsp;&nbsp;\n" +
                "            <span class=\"vw\">\n" +
                "\t\t\t<i class=\"pb-icons\"></i>\n" +
                "            浏览("+
                kdbList.getCount()+
                ")\n" +
                "            </span>\n" +
                "        </div>"+
                kdbList.getContent()+
                "</body>"+
                "</html>";
        return  html;
    }
}
